<!-- 
 Copyright 2009 University of  Washington
This code is released under the GPL version 3 - with a copy of the license included
@author Maya Hao Li
 -->
<%@ page contentType="text/html; charset=ISO-8859-1" language="java"%>
<%@ page import="seedpod.model.custom.SeedpodUser"%>
<%@ page import="seedpod.webapp.LimsSession"%>
<%@ page import="seedpod.webapp.view.Page"%>
<%@ page import="seedpod.rdb.PManager"%>
<%@ page import="seedpod.webapp.view.htmlwidget.*"%>
<%@ page import="java.util.*"%>
<%@ page import="seedpod.model.SeedpodDO"%>
<%@ page import="seedpod.exceptions.*"%>
<%@ page import="seedpod.kb2db.ClsMap"%>
<%@ page import="seedpod.kb2db.SlotMap"%>
<%@ page import="seedpod.model.InstanceRelationship" %>
<%
	Page spPage = new Page();
	spPage.addJsLibrary( "/javascript/lib/scriptaculous/src/effects.js" );
	spPage.addJsLibrary( "/javascript/lib/scriptaculous/src/scriptaculous.js" );
	SeedpodUser user = LimsSession.getSession( request ).getUser();
	LimsSession spSession = new LimsSession( request );
	PManager manager = spSession.getDataManager();

	String relationship = (String)request.getParameter( "relationship" );
	String targetClsName = (String)request.getParameter( "targetCls" );
	String srcClsName = (String)request.getParameter( "srcCls" );
	String srcOidStr = (String)request.getParameter( "sourceOid" );
	Integer srcOid = (srcOidStr != null) ? Integer.decode( srcOidStr ) : null;

	ClsMap m_srcCls = manager.getModelMap().getClsMap( srcClsName );
	SlotMap m_relationship = m_srcCls.getSlotMap( relationship );
	ClsMap m_tgtCls = manager.getModelMap().getClsMap( targetClsName );
	String filter = "";
	InstanceRelationship instanceRelation = new InstanceRelationship( m_relationship, srcOid );
	SlotMap m_inverseSlot = instanceRelation.getSlotInverse();
	
	if ( targetClsName != null ) {
		Table objTable = new Table();
		TableRow currRow;
		ArrayList<Integer> targetObjects; 
		try {
			// query available target class objects
			targetObjects = instanceRelation.getPotentialTargetObjects(); 
			
			if ( targetObjects == null || targetObjects.size() == 0 ) {
				String createUrl = "../lims/instance.jsp?clsName=" + targetClsName + "&action=create&" + m_inverseSlot.getName() + "=" + srcOid;
				out.println( "No " + targetClsName + " in the database yet. Would you like to create a new one?" );
				out.println( "<br> <input class=\"commandButton\" type=\"button\" value=\"yes\" onclick=\"window.location='" + createUrl + "';\">" );
			} else {
				out.println( "Select " + targetClsName + " from the list below" );
				for ( Integer obj : targetObjects ) {
					String cbName = instanceRelation.getRelationshipSlot( ).getName() ;
					CheckBoxInput cb = new CheckBoxInput( cbName, obj.toString() );

					currRow = objTable.addRow();
					currRow.addCell( cb );
					currRow.addCell( "<a href=\"instance.jsp?oid=" + obj + "\">" + obj  + "</a>" );
				}
				out.println( "<div style=\"width:300px;\"" );
				out.println( "<form action=\"\" id=\"selectInstances" + relationship + "\">" );
				out.println( objTable.render() );
				out.println( "<input type=\"submit\" value=\"add\" name=\"Add Selected\">" );
				out.println( "</form>" );
				out.println( "</div>" );
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.println( e.getMessage() );
		}
	}
%>
